<?php

namespace app\admin\controller;

use My\MyClass;
use think\Db;
use think\Request;

class jiaose extends MyClass
{
    public function index()
    {
        $result = Db::table('jiaose')->order('id asc')->select();

        $row = Db::field(['j.id' => 'jid', 'j.name' => 'jn', 'p.permission' => 'pn'])
            ->table(['jiaose' => 'j', 'permission' => 'p', 'jiaose_q' => 'jq'])
            ->where('j.id=jq.jid and p.id=jq.qid')
            ->select();

//        dump($result);
//        halt($row);
        for ($i = 0; $i < count($result); $i++) {
            foreach ($row as $v) {
                if ($result[$i]['id'] == $v['jid']) {

                    $result[$i]['per'][] = $v['pn'];
                }
            }
        }
        return view('admin@jiaose/index', [
            'data' => $result,
        ]);
    }

    public function jsadd()
    {
        return view('admin@jiaose/jsadd');
    }


    public function jsdoadd(Request $request)
    {

        $result = $request->get();
        $row = Db::name('jiaose')->insertGetId($result);
        $ID = Db::table('jiaose_q')->insert(['jid'=>$row,'qid'=>'2']);
        if ($row > 0) {
            return $this->success('添加成功', '/users/jiaose');
        } else {
            return $this->error('添加失败,请重试');

        }
    }

    public function look($id)
    {

        $result = Db::field(['j.name' => 'jn', 'p.permission' => 'pn'])
            ->table(['jiaose' => 'j', 'permission' => 'p', 'jiaose_q' => 'jq'])
            ->where('j.id=jq.jid and p.id=jq.qid and j.id =' . $id)
            ->select();

        if ($result == null) {
            $this->error('当前角色不具备任何权限!');
        }
        $name = null;
        $quanxian = null;

        for ($i = 0; $i < count($result); $i++) {
            $name = $result['0']['jn'];
            $quanxian .= $result[$i]['pn'] . '-';
        }
        $quanxian = substr($quanxian, 0, -1);

        return view('admin@jiaose/quanxian', [
            'name' => $name,
            'quanxian' => $quanxian
        ]);
    }
}
